package edu.bit.water.service;

import edu.bit.water.dao.MetaDischargeDao;
import edu.bit.water.entity.MetaDischarge;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * MetaDischargeController的service层
 * author: Page
 */
@Service
public class MetaDischargeService {

    @Autowired
    private MetaDischargeDao metaDischargeDao;

    /**
     * 查询MetaDischarge
     */
    public List<MetaDischarge> queryDischarge() {
        return metaDischargeDao.queryDischarge();
    }


    /**
     * 插入新的metaDischarge
     */
    public Map<String, Object> insertMetaDischargeService(MetaDischarge metaDischarge) {
        metaDischarge.dateChange();
        Map<String, Object> result = new HashMap<>();
        Integer num = metaDischargeDao.getMaxId();
        if (num != null) {
            metaDischarge.setId(num + 1);
        }
        else {
            metaDischarge.setId(0);
        }
        int tmp = metaDischargeDao.insertMetaDischarge(metaDischarge);
        if (tmp == 1) {
            result.put("status", "success");
        }else {
            result.put("status", "failed");
        }
        return result;
    }


    /**
     * 编辑提交
     */
    public Map<String, Object> updateMetaDischargeService(MetaDischarge metaDischarge) {
        metaDischarge.dateChange();
        Map<String, Object> result = new HashMap<>();
        int tmp = metaDischargeDao.update(metaDischarge);
        if(tmp == 0) {
            result.put("status", "failed");
        }
        else {
            result.put("status", "success");
        }
        return result;
    }

    /**
     * 删除metaDischarge
     * author： page
     */
    public Map<String, Object> deleteMetaDischarges(List<String> metaDischarges) {
        Map<String, Object> tmpMap = new HashMap<>();
        boolean flag = true;
        for (int i = 0; i < metaDischarges.size(); i++) {
            int result = metaDischargeDao.delete(Integer.parseInt(metaDischarges.get(i)));
            if(result == 0) {
                tmpMap.put(metaDischarges.get(i), "failed");
                flag = false;
            }
            else {
                tmpMap.put(metaDischarges.get(i), "success");
            }
        }
        if (flag) {
            tmpMap.put("status", "success");
        }
        else {
            tmpMap.put("status", "failed");
        }
        return tmpMap;
    }
}
